#django.core.exceptions import PermissionDenied
#from django.contrib.auth.decorators import login_required
#from django.http import HttpResponse
#from django.utils import simplejson
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse
from django.http import Http404
from django.shortcuts import render_to_response, get_object_or_404, HttpResponseRedirect
from django.template import RequestContext

from fragrances.models import Fragrance, Paper, News
from fragrances.forms import *


def fragrance_view(request, id):
    fragrance_object = get_object_or_404(Fragrance, id=id)
    #if request.is_ajax():
    #  return HttpResponse(simplejson.dumps(fragrance_object), mimetype='application/javascript')
    return render_to_response('fragrance_view.html', {'fragrance_object':fragrance_object,}, context_instance=RequestContext(request))


#@login_required    
def fragrance_create(request):
    if request.method == 'POST':
        form = FragranceCreate(request.POST)
        if form.is_valid():
            item = form.save()
            return HttpResponseRedirect(reverse('fragrances:fragrance_view', kwargs={'id':item.id}))
    else:
        form = FragranceCreate()
    return render_to_response('fragrance_create.html', {'form': form, }, context_instance=RequestContext(request))


#@login_required    
def fragrance_edit(request, id=id):
    fragrance_object = get_object_or_404(Fragrance, id=id)
    #if request.user != fragrance_object.user:
    #   raise PermissionDenied
    if request.method == 'POST':
        form = FragranceEdit(request.POST, instance=fragrance_object)
        if form.is_valid():
            item = form.save()
            return HttpResponseRedirect(reverse('fragrances:fragrance_view', kwargs={'id':item.id}))
    else:
        form = FragranceEdit(instance=fragrance_object)
    return render_to_response('fragrance_edit.html', {'form': form, 'fragrance_object': fragrance_object}, context_instance=RequestContext(request))


def fragrance_list(request):
    fragrance_set = Fragrance.objects.all() # .order_by('-date')           
    return render_to_response('fragrance_list.html',{'fragrance_set': fragrance_set,}, context_instance=RequestContext(request))


def paper_view(request, id):
    paper_object = get_object_or_404(Paper, id=id)
    #if request.is_ajax():
    #  return HttpResponse(simplejson.dumps(paper_object), mimetype='application/javascript')
    return render_to_response('paper_view.html', {'paper_object':paper_object,}, context_instance=RequestContext(request))


#@login_required    
def paper_create(request):
    if request.method == 'POST':
        form = PaperCreate(request.POST)
        if form.is_valid():
            item = form.save()
            return HttpResponseRedirect(reverse('fragrances:paper_view', kwargs={'id':item.id}))
    else:
        form = PaperCreate()
    return render_to_response('paper_create.html', {'form': form, }, context_instance=RequestContext(request))


#@login_required    
def paper_edit(request, id=id):
    paper_object = get_object_or_404(Paper, id=id)
    #if request.user != paper_object.user:
    #   raise PermissionDenied
    if request.method == 'POST':
        form = PaperEdit(request.POST, instance=paper_object)
        if form.is_valid():
            item = form.save()
            return HttpResponseRedirect(reverse('fragrances:paper_view', kwargs={'id':item.id}))
    else:
        form = PaperEdit(instance=paper_object)
    return render_to_response('paper_edit.html', {'form': form, 'paper_object': paper_object}, context_instance=RequestContext(request))


def paper_list(request):
    paper_set = Paper.objects.all() # .order_by('-date')           
    return render_to_response('paper_list.html',{'paper_set': paper_set,}, context_instance=RequestContext(request))


def news_view(request, id):
    news_object = get_object_or_404(News, id=id)
    #if request.is_ajax():
    #  return HttpResponse(simplejson.dumps(news_object), mimetype='application/javascript')
    return render_to_response('news_view.html', {'news_object':news_object,}, context_instance=RequestContext(request))


#@login_required    
def news_create(request):
    if request.method == 'POST':
        form = NewsCreate(request.POST)
        if form.is_valid():
            item = form.save()
            return HttpResponseRedirect(reverse('fragrances:news_view', kwargs={'id':item.id}))
    else:
        form = NewsCreate()
    return render_to_response('news_create.html', {'form': form, }, context_instance=RequestContext(request))


#@login_required    
def news_edit(request, id=id):
    news_object = get_object_or_404(News, id=id)
    #if request.user != news_object.user:
    #   raise PermissionDenied
    if request.method == 'POST':
        form = NewsEdit(request.POST, instance=news_object)
        if form.is_valid():
            item = form.save()
            return HttpResponseRedirect(reverse('fragrances:news_view', kwargs={'id':item.id}))
    else:
        form = NewsEdit(instance=news_object)
    return render_to_response('news_edit.html', {'form': form, 'news_object': news_object}, context_instance=RequestContext(request))

def news_list(request):
    news_set = News.objects.all() # .order_by('-date')           
    return render_to_response('news_list.html',{'news_set': news_set,}, context_instance=RequestContext(request))
